<?php
namespace App\Logic\Amazon\AdsApi\ReportService;

use App\Logic\Amazon\AdsApi\Report;
use App\Models\AmazonAds\AdsDailyReport;
use Illuminate\Support\Facades\DB;
trait DealReport
{
    /**
     * 解析报告
     * @param $lines
     * @param $accountId
     * @param $countryCode
     * @param $table
     * @return bool
     */
    public static function parseReport($lines,$accountId,$countryCode,$table): bool
    {
        ini_set('memory_limit', '200M');
        set_time_limit(0);
        $i = 0;
        $num = 2000;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            try {
                DB::connection('mysql_amazon_ads')->beginTransaction();
                foreach($_lines as $k => $line){
                    if ($table == 'ads_sp_product') { //广告产品表
                        if(empty($line['advertisedSku'])){ //advertisedSku字段为空
                            unset($_lines[$k]);
                            continue;
                        }
                    }
                    $line['accountId'] = $accountId;
                    $line['countryCode'] = $countryCode;
                    $line['created_at'] = date('Y-m-d H:i:s');
                    $line['updated_at'] = date('Y-m-d H:i:s');
                    $_lines[$k] = $line;
                }
                DB::connection('mysql_amazon_ads')->table($table)->insert($_lines);
                DB::connection('mysql_amazon_ads')->commit();
            } catch (\Throwable $e) {
                report('[账号ID:'.$accountId.'国家:'.$countryCode.",_parseDownloadData table ".$table." fail]".$e->getMessage());
                return false;
            }
        }
        return true;
    }

    /**
     * 请求报告
     * @param $config
     * @param $profileId
     * @param $countryCode
     * @param $params
     * @param $configuration
     * @return mixed
     */
    public static function createReport($config,$profileId,$countryCode,$params,$configuration): mixed
    {
        $params = self::_extractedReportParameters($params,$configuration);
        $data = Report::createReport($config,$profileId,$params);
        if(is_null($data)){
            return false;
        }
        $re = AdsDailyReport::query()->create([
            'account_id' => $config['accountId'],
            'country_code' => $countryCode,
            'report_type_id' => $configuration['reportTypeId'],
            'profile_id' => $profileId,
            'time_unit' => !array_key_exists($params['configuration']['timeUnit'],AdsDailyReport::$_timeUnit) ? 3 : AdsDailyReport::$_timeUnit[$params['configuration']['timeUnit']],
            'start_date' => $params['startDate'],
            'end_date' => $params['endDate'],
            'report_id' => $data['reportId'],
            'create_report_status' => !array_key_exists($data['status'],AdsDailyReport::$_status) ? 5 : AdsDailyReport::$_status[$data['status']],
        ]);
        if($re){
            return AdsDailyReport::$_createReportStatus[$re->create_report_status];
        }else{
            return false;
        }
    }

    /**
     * 处理请求报告参数
     * @param array $params
     * @param array $paramsConfiguration
     * @return array
     */
    private static function _extractedReportParameters(array $params,array $paramsConfiguration): array
    {
        if(array_key_exists('timeUnit',$params)){
            //(timeUnit: DAILY or SUMMARY)
            if($params['timeUnit']){
                $paramsConfiguration['timeUnit'] = $params['timeUnit'];
            }
            unset($params['timeUnit']);
        }
        $params['configuration'] = $paramsConfiguration;
        return $params;
    }
}
